package com.bjsxt.sm.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.sm.admin.service.AdminService;
import com.bjsxt.sm.common.controller.BaseController;
import com.bjsxt.sm.common.util.ConstatFinalUtil;
import com.bjsxt.sm.admin.pojo.*;

/*
 * 登录处理controller
 */
@Controller
public class LoginController extends BaseController
{    
	@Resource
	AdminService adminServiceI;
	
	@RequestMapping("/login")
	public String login(){
    	ConstatFinalUtil.LOGGER.info("========login======");

		return "/main/login" ; 
	}
	
    @RequestMapping("/loginSubmit")
	public String loginSubmit(String email,String password,HttpServletRequest request,HttpSession session){
    	ConstatFinalUtil.LOGGER.info("========loginSubmit======email{},password{}===",email,password);
		/* 接收页面的信息 */
		/* 查询数据库是否存在 
		 * 按照email查询
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("email", email);
		AAdmins admins = this.adminServiceI.findOneAdmin(condMap);
		if(admins != null)
		{
			request.setAttribute("email", email);
			/* 邮箱肯定正确+存在 */
			if(admins.getPassword().equalsIgnoreCase(password))
			{
				/* 密码正确 */
				/* 判断一下状态 */
				if(admins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
				{
					/* 登陆成功 */
					session.setAttribute("admins", admins);				
					/* 客户端跳转 */
					return "redirect:/main/main.html" ; 
				}else
				{
					/* 状态被禁用了 */
					this.info = "状态被禁用" ; 
				}
			}else
			{
				/* 密码不正确 */
				this.info = "密码不正确" ; 
			}
		}else
		{
			/* 邮箱不存在 */
			this.info = "邮箱不存在" ;
		}
		request.setAttribute("info", info);
		return this.login() ; 
	}
	}
	
